package br.com.unibratec.view;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.Toast;
import br.com.unibratec.negocio.Evento;
import br.com.unibratec.negocio.EventoAdapter;
import br.com.unibratec.negocio.EventoDAO;
import br.com.unibratec.service.WidgetService;
import br.com.unibratec.util.AgendaUtil;

public class ListaEventosActivity extends ListActivity {

	private EventoDAO eventoDAO;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		eventoDAO = EventoDAO.getInstace(this);
		this.registerForContextMenu(this.getListView());
		AgendaUtil.getInstace().setConfig(this, this.getApplicationContext()).atualizarNotificacoes(this.eventoDAO.listaAtualizada());
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.atualizarLista();
	}

	private void atualizarLista() {
		Log.e("JDH", "atualizarLista");

		String filtro = null;
		int paramFiltro = this.getIntent().getIntExtra("filtro", -1);
		if (paramFiltro > -1) {
			filtro = "ativo = " + paramFiltro;
		}
		EventoAdapter adapter = new EventoAdapter(this, eventoDAO.pesquisar(filtro));
		this.setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView listView, View view, int position, long id) {
		super.onListItemClick(listView, view, position, id);

		Evento evento = (Evento) listView.getAdapter().getItem(position);
		Intent intent = new Intent(this, CadastrarEventosActivity.class);
		intent.putExtra("evento", evento);

		this.startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, "Cadastrar").setIcon(android.R.drawable.ic_menu_add);
		menu.add(0, 2, 0, "Sair").setIcon(android.R.drawable.ic_menu_close_clear_cancel);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, view, menuInfo);
		menu.add(0, 3, 0, "Excluir");
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			this.startActivity(new Intent(this, CadastrarEventosActivity.class));
			break;

		case 2:
			this.finish();
			break;

		case 3:
			AdapterContextMenuInfo meuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
			Evento evento = (Evento) this.getListAdapter().getItem(meuInfo.position);
			eventoDAO.excluir(evento);

			AgendaUtil.getInstace().setConfig(this, this.getApplicationContext()).cancelarAgendamento((int) evento.getId());

			this.atualizarLista();
			Intent it = new Intent(this, WidgetService.class);
			it.setAction(WidgetService.ACTION_LOAD_DATA);
			this.startService(it);
			break;

		default:
			Toast.makeText(this, "Ocorreu um erro", Toast.LENGTH_SHORT).show();
			break;

		}
		return super.onMenuItemSelected(featureId, item);
	}

}
